package com.lianboke.link.ad.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lianboke.link.ad.R;
import com.lianboke.link.ad.VolleyUtils.HttpCallBack;
import com.lianboke.link.ad.VolleyUtils.HttpUtil;
import com.lianboke.link.ad.app.MyApplication;
import com.lianboke.link.ad.beans.UserAccountModel;
import com.lianboke.link.ad.common.CommonParams;
import com.lianboke.link.ad.utils.DeviceUtils;
import com.lianboke.link.ad.utils.ToastUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

import butterknife.BindView;

/**
 * 个人账号信息
 */
public class UserAccountActivity extends BaseActivity {

    //提现功能
    @BindView(R.id.tv_user_account)
    public TextView tv_user_account;

    //提现金额
    @BindView(R.id.et_tixian_money)
    public EditText et_tixian_money;

    //提现按钮
    @BindView(R.id.btn_submit)
    public Button btn_submit;

    @BindView(R.id.tv_user_name_value)
    public TextView tv_user_name_value;

    //提现功能
    @BindView(R.id.tv_user_zhifubao_value)
    public TextView tv_user_zhifubao_value;

    @BindView(R.id.tv_user_zhifubao_name_value)
    public TextView tv_user_zhifubao_name_value;

    @BindView(R.id.tv_user_phone_value)
    public TextView tv_user_phone_value;

    @BindView(R.id.tv_user_qq_value)
    public TextView tv_user_qq_value; //提现功能

    @BindView(R.id.tv_user_email_value)
    public TextView tv_user_email_value;

    @BindView(R.id.tv_user_bank_name_value)
    public TextView tv_user_bank_name_value;

    @BindView(R.id.tv_user_bank_card_value)
    public TextView tv_user_bank_card_value;

    @BindView(R.id.tv_user_bank_address_value)
    public TextView tv_user_bank_address_value;

    @BindView(R.id.tv_user_vpn_vpn_enddate_value)
    TextView tv_user_vpn_vpn_enddate_value;

    @BindView(R.id.tv_user_vpn_vpn_startdate_value)
    TextView tv_user_vpn_vpn_startdate_value;


    @BindView(R.id.tv_user_vpn_name_value)
    TextView tv_user_vpn_name_value;
    @BindView(R.id.tv_user_vpn_permonthpay_value)
    TextView tv_user_vpn_permonthpay_value;

    @BindView(R.id.tv_user_vpn_fukuan_flag)
    TextView tv_user_vpn_fukuan_flag;
    @BindView(R.id.iv_user_vpn_fukuan_value)
    ImageView iv_user_vpn_fukuan_value;

    //积分信息
    @BindView(R.id.tv_user_jifen_youmi_value)
    TextView tv_user_jifen_youmi_value;
    @BindView(R.id.tv_user_jifen_wanpu_value)
    TextView tv_user_jifen_wanpu_value;



    @BindView(R.id.loading_progress)
    public ProgressBar loading_progress;


    private UserAccountModel userAccountModel;

    private Animation alpha ;
    @Override
    public int getContentViewId() {
        return R.layout.activity_user_account;
    }

    @Override
    public void initActivityParams(Bundle savedInstanceState) {
        setTitleBackShow(true);
        setTitleContent("个人信息");
        setTitleMoreShow(true);

        //提款
        btn_submit.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                submitTiXianInfo();
            }
        });

        //获取账单详情
        getUserAccountInfo();
    }

    @Override
    public void TitleMoreActivity() {
        Intent mIntent = new Intent();
        mIntent.setClass(actContext,UserTiXianRecordActivity.class);
        startActivity(mIntent);
    }
    @Override
    public void TitleBackFinish() {
        super.TitleBackFinish();
        overridePendingTransition(R.anim.in_from_left,R.anim.out_to_right);
    }
    /**
     * 显示加载进度
     */
    private void showLoadingProgress(){
        if(loading_progress!=null && loading_progress.getVisibility()!= View.VISIBLE){
            loading_progress.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏加载进度
     */
    private void hideLoadingProgress(){
        if(loading_progress!=null && loading_progress.getVisibility()!=View.GONE){
            loading_progress.setVisibility(View.GONE);
        }
    }

    /**
     * 获取用户信息
     */
    public void getUserAccountInfo(){
        showLoadingProgress();
        HashMap<String,String> params = new HashMap<String,String>();
        params.put("deviceid", DeviceUtils.getImei(appContext));
        params.put("version",DeviceUtils.getVersionName(actContext));
        params.put("nettypename",DeviceUtils.getDeviceAc());
        params.put("city", MyApplication.strCity);
        HttpUtil.getInstance().request_get(CommonParams.URL_WEB_GET_USER_FIND_ACCOUNT, params, new HttpCallBack<UserAccountModel>() {
            @Override
            public void onSuccess(UserAccountModel data) {
                userAccountModel = data;
                hideLoadingProgress();
                showUserAccountInfo();
            }

            @Override
            public void onFail(String msg) {
                hideLoadingProgress();
            }
        });
    }

    /**
     * 显示用户嘻嘻
     */
    private void  showUserAccountInfo(){
        if(userAccountModel!=null){
            tv_user_account.setText(tv_user_account.getText()+userAccountModel.getWeitimoney()+"(元)");
            tv_user_name_value.setText(userAccountModel.getUsername());

            tv_user_zhifubao_value.setText(userAccountModel.getZhifubaoaccount());
            tv_user_zhifubao_name_value.setText(userAccountModel.getZhifubaorealname());
            tv_user_phone_value.setText(userAccountModel.getPhone());

            tv_user_qq_value.setText(userAccountModel.getQq());
            tv_user_email_value.setText(userAccountModel.getEmail());

            tv_user_bank_name_value.setText(userAccountModel.getBankaccountname());
            tv_user_bank_card_value.setText(userAccountModel.getBankcard());
            tv_user_bank_address_value.setText(userAccountModel.getBankadress());

            tv_user_vpn_vpn_enddate_value.setText(userAccountModel.getVpndeadline());
            tv_user_vpn_vpn_startdate_value.setText(userAccountModel.getVpnstartdate());
            tv_user_vpn_name_value.setText(userAccountModel.getUvpnname());
            tv_user_vpn_permonthpay_value.setText(userAccountModel.getVpnpermonthpay()+"(元)");
            alpha= AnimationUtils.loadAnimation(actContext, R.anim.anim_alpha);//获取透明度变化动画资源
            //用户等待时设置默认图片的动画
            alpha.setRepeatCount(Animation.INFINITE);//循环显示
            iv_user_vpn_fukuan_value.startAnimation(alpha);
            loadUserVpnFuKuanImage();

            tv_user_jifen_youmi_value.setText(userAccountModel.getMiyoujifen());
            tv_user_jifen_wanpu_value.setText(userAccountModel.getWanpujifen());
        }
    }

    /**
     * 体现接口
     */
    private void  submitTiXianInfo(){
        String strMoney = et_tixian_money.getText().toString().trim();
        if(!isNumeric(strMoney)){
            ToastUtil.showShort(actContext,"请输入正确的提现金额！");
            return;
        }
        int iMoneyValue = Integer.valueOf(strMoney);
        if(iMoneyValue<100 || iMoneyValue>1000){
            ToastUtil.showShort(actContext,"请输入正确的提现金额！");
            return ;
        }

        showLoadingProgress();
        HashMap<String,String> params = new HashMap<String,String>();
        params.put("deviceid", DeviceUtils.getImei(appContext));
        params.put("version",DeviceUtils.getVersionName(actContext));
        params.put("nettypename",DeviceUtils.getDeviceAc());
        params.put("city", MyApplication.strCity);
        params.put("money",String.valueOf(iMoneyValue));
        HttpUtil.getInstance().request_get(CommonParams.URL_WEB_SUBMIT_DRAW_MONEY, params, new HttpCallBack<UserAccountModel>() {
            @Override
            public void onSuccess(UserAccountModel data) {
                userAccountModel = data;
                hideLoadingProgress();
                showUserAccountInfo();
            }

            @Override
            public void onFail(String msg) {
                hideLoadingProgress();
                showAlerDialog(msg);
            }
        });
    }

    /**
     * 判断是否为整数
     * @param s
     * @return
     */
    public  boolean isNumeric(String s) {
        if (!TextUtils.isEmpty(s)) {
            return s.matches("^[0-9]*$");
        }else {
            return false;
        }
    }

    /**
     * 加载图片
     */
    private void loadUserVpnFuKuanImage(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                //从网络上获取图片
                final Bitmap bitmap = getPicture(userAccountModel.getVpnpayimage());
                try {
                    Thread.sleep(1000);//线程休眠两秒钟
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                alpha.setRepeatCount(0);//循环显示
                //发送一个Runnable对象
                try {
                    iv_user_vpn_fukuan_value.post(new Runnable() {
                        @Override
                        public void run() {
                            if (bitmap != null && iv_user_vpn_fukuan_value != null) {
                                iv_user_vpn_fukuan_value.setImageBitmap(bitmap);//在ImageView中显示从网络上获取到的图片
                            }
                        }
                    });
                }catch (Exception e){

                }
            }
        }).start();//开启线程

    }
    /*
   * 功能:根据网址获取图片对应的Bitmap对象
   * @param path
   * @return Bitmap
   * */
    public Bitmap getPicture(String path){
        Bitmap bm=null;
        URL url;
        try {
            url = new URL(path);//创建URL对象
            URLConnection conn=url.openConnection();//获取URL对象对应的连接
            conn.connect();//打开连接
            InputStream is=conn.getInputStream();//获取输入流对象
            bm= BitmapFactory.decodeStream(is);//根据输入流对象创建Bitmap对象
        } catch (MalformedURLException e1) {
            e1.printStackTrace();//输出异常信息
        }catch (IOException e) {
            e.printStackTrace();//输出异常信息
        }
        return bm;
    }
}
